package com.tao.power.Utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Validator;
import com.tao.power.Dao.LoginMapper;
import com.tao.power.Dao.RolerMapper;
import com.tao.power.Dao.UserMapper;
import com.tao.power.Pojo.Menu;
import com.tao.power.Pojo.Roler;
import com.tao.power.Pojo.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

@Component
public class verifyUtils {


    @Resource
    private UserMapper userMapper;
    @Resource
    private RolerMapper rolerMapper;

    private final String pattern = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\\\.[a-zA-Z0-9]+";

    public void userVerify(User user){
        if (Validator.isEmpty(user.getUsername())){
            throw new SystemExceptionNew("用户名不能为空");
        }

        if (user.getUsername().equals(userMapper.queryUsername(user.getUsername()))){
            throw new SystemExceptionNew("已存在该用户名!");
        }

        if(user.getUsername().length()>20){
            throw new SystemExceptionNew("用户名过长!");
        }
        if (user.getUsername().length()<5){
            throw new SystemExceptionNew("用户名过短!");
        }

        if (user.getPassword().length()<5){
            throw new SystemExceptionNew("密码过短!");
        }

        if (user.getPassword().length()>20){
            throw new SystemExceptionNew("密码过长!");
        }

        if (Validator.isEmpty(user.getName())){
            throw new SystemExceptionNew("姓名不能为空");
        }
        if (Validator.isEmpty(user.getTelephone())){
            throw new SystemExceptionNew("联系电话不能为空");
        }
        if(Validator.isMatchRegex(pattern,user.getEmail())){
            throw new SystemExceptionNew("邮箱格式不正确");
        }
    }

    public  void rolesVerify(Roler roler){
        if (Validator.isEmpty(roler.getRolerId())){
            throw new SystemExceptionNew("角色编码不能为空");
        }
        if (roler.getRolerName().equals(rolerMapper.queryRolesName(roler.getRolerName()))){
            throw new SystemExceptionNew("已存在该角色!");
        }
        if (Validator.isEmpty(roler.getRolerName())){
            throw new SystemExceptionNew("角色编码不能为空");
        }
    }

    public  void menuVerify(Menu menu){
        if (Validator.isEmpty(menu.getMenuName())){
            throw new SystemExceptionNew("菜单不能为空");
        }
    }

    public boolean importVerify(User user){
        if (Validator.isEmpty(user.getUsername())){
            return false;
        }

        if (user.getUsername().equals(userMapper.queryUsername(user.getUsername()))){
            return false;
        }

        if(user.getUsername().length()>20){
            return false;
        }
        if (user.getUsername().length()<5){
            return false;
        }

        if (user.getPassword().length()<5){
            return false;
        }

        if (user.getPassword().length()>20){
            return false;
        }

        if (Validator.isEmpty(user.getName())){
            return false;
        }
        if (Validator.isEmpty(user.getTelephone())){
            return false;
        }
        if(Validator.isMatchRegex(pattern,user.getEmail())){
            return false;
        }
        return true;
    }
}
